/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License. You may obtain a copy of the License at
	http://www.mozilla.org/MPL/
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import com.bourre.structures.Point;
import com.bourre.structures.Rectangle;

import fever.app.context.abstract.ContextContainer;
import fever.app.context.abstract.IContextContainer;
import fever.app.context.haxe.HaxeContext;
import fever.app.context.haxe.HaxeMethod;
import fever.core.ApplicationState;
import fever.core.FeverRoot;
import fever.events.ApplicationEvent;
import fever.Fever;

import swhx.Api;

/**
 * Builds a <strong>Haxe container</strong> for application.
 * 
 * @see fever.app.context.browser.BrowserContext
 * 
 * @author Romain Ecarnot
 */
class fever.app.context.haxe.HaxeContainer extends ContextContainer implements IContextContainer
{
	//-------------------------------------------------------------------------
	// Private properties
	//-------------------------------------------------------------------------
	
	private var _caption : String;	private var _displayState : Boolean;
	private var _lastState : ApplicationState;
	
	
	//-------------------------------------------------------------------------
	// Public Properties
	//-------------------------------------------------------------------------
	
	public static var setTitleMETHOD = new HaxeMethod( 'setTitle' );
	public static var getTitleMETHOD : HaxeMethod = new HaxeMethod( 'getTitle' );
	public static var setLocationMETHOD : HaxeMethod = new HaxeMethod( 'setLocation' );
	public static var getLocationMETHOD : HaxeMethod = new HaxeMethod( 'getLocation' );
	public static var setSizeMETHOD : HaxeMethod = new HaxeMethod( 'setSize' );
	public static var getSizeMETHOD : HaxeMethod = new HaxeMethod( 'getSize' );
	public static var setFullScreenMETHOD : HaxeMethod = new HaxeMethod( 'setFullScreen' );
	public static var getFullScreenMETHOD : HaxeMethod = new HaxeMethod( 'getFullScreen' );
	
		
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	/**
	 * Constructor.
	 */
	public function HaxeContainer()
	{
		super();
		
		_displayState = false;
		
		Stage.addListener( this );
	}	
	
	/** 
	 * Set the container title.
	 */
	public function setTitle(  newtitle : String  ) : Void 
	{
		if( _caption != newtitle )
		{
			_caption = newtitle;
			Api.call( HaxeContext.getHaxeMethod( setTitleMETHOD ), _caption );
		}
	}
	
	/** 
	 * Returns the container title.
	 */
	public function getTitle() : String
	{
		_caption = Api.call( HaxeContext.getHaxeMethod( getTitleMETHOD ) );
		
		if( !_caption )
		{
			var tx : TextField = FeverRoot.LEVEL["_title"];
			_caption = ( tx != undefined ) ? tx.text : "";
		}
		return _caption;
	}
	
	/** 
	 * Set the container position.
	 */
	public function setLocation( x : Number, y : Number ) : Void
	{
		Api.call( HaxeContext.getHaxeMethod( setLocationMETHOD ), x, y );
	}
	
	/** 
	 * Returns the container title.
	 */
	public function getLocation() : Point
	{
		var o : Object = Api.call( HaxeContext.getHaxeMethod( getLocationMETHOD ) );		return new Point( o.x, o.y );
	}
	
	/** 
	 * Set the container size.
	 */
	public function setSize( width : Number, height : Number ) : Void
	{
		Api.call( HaxeContext.getHaxeMethod( setSizeMETHOD ), width, height );
	}
	
	/** 
	 * Returns the container title.
	 */
	public function getSize() : Rectangle
	{
		var size : Object = Api.call( HaxeContext.getHaxeMethod( getSizeMETHOD ) );
		return new Rectangle( size.width, size.height, 0, 0 );
	}
	
	/**
	 * 	Sets current application in fullscreen mode if passed-in {@code enabled} 
	 * 	is {@code true}, or in normal mode if {@code false}.
	 */
	public function setFullScreenMode( enabled : Boolean ) : Void
	{
		if( _displayState != enabled )
		{
			if( enabled ) _lastState = Fever.application.state;
			
			Api.call( HaxeContext.getHaxeMethod( setFullScreenMETHOD ), enabled );
			
			_displayState = enabled;
			
			Fever.application.broadcastEvent( 
				new ApplicationEvent( 
					ApplicationEvent.onStateChangeEVENT,
					( _displayState ) ? ApplicationState.FULLSCREEN : _lastState
				 ) 
			);
		}
	}
	
	/**
	 * Indicates if current application is on fullscreen mode {@code true} or 
	 * in normal mode {@code false}.
	 */
	public function isFullScreen() : Boolean
	{
		return Api.call( HaxeContext.getHaxeMethod( getFullScreenMETHOD ) );	
	}
	
	/** 
	 * TODO Returns application argument associated with {@code id} ID.
	 */
	public function getArgument( id ) : String
	{
		return null;
	}
	
	
	//-------------------------------------------------------------------------
	// Private implementation
	//-------------------------------------------------------------------------
	
	private function onResize() : Void
	{
		Fever.application.broadcastEvent( new ApplicationEvent( ApplicationEvent.onResizeEVENT ) );	
	}
	
	
}